package com.dev.utils.app;

import ohos.agp.utils.Color;
import ohos.event.notification.NotificationHelper;
import ohos.event.notification.NotificationRequest;
import ohos.event.notification.NotificationSlot;
import ohos.rpc.RemoteException;

import static ohos.event.notification.NotificationSlot.LEVEL_HIGH;

/**
 * 通知管理工具类
 */
public class NotificationUtils {

    /**
     * 发布通知的方法
     *
     * @param content
     * @param title
     * @param text
     */
    public static void publishNo(NotificationRequest request, NotificationRequest.NotificationNormalContent content,
                                 String title, String text) {
        content.setTitle(title)
                .setText(text);
        NotificationRequest.NotificationContent notificationContent
                = new NotificationRequest.NotificationContent(content);
        request.setContent(notificationContent);
        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
    public static NotificationSlot slot;
    public static void initNotificationSlot() {
        slot = new NotificationSlot("slot_001", "slot_default", NotificationSlot.LEVEL_MIN); // 创建notificationSlot对象
        slot.setDescription("NotificationSlotDescription");
        slot.setEnableLight(true);
        slot.setEnableVibration(true); // 设置振动提醒
        slot.setLockscreenVisibleness(NotificationRequest.VISIBLENESS_TYPE_PUBLIC);//设置锁屏模式
        slot.setEnableLight(true); // 设置开启呼吸灯提醒
        slot.setLedLightColor(Color.RED.getValue());// 设置呼吸灯的提醒颜色
        slot.setLevel(LEVEL_HIGH);
        try {
            NotificationHelper.addNotificationSlot(slot);
        } catch (RemoteException ex) {
//            HiLog.warn(LABEL, "addNotificationSlot occur exception.");
        }
    }

    public static int notificationId=1;
    public static void publishNotification() {
        NotificationRequest request = new NotificationRequest(notificationId);
        request.setSlotId(slot.getId());
        String title = "机密信息";
        String text = "有内鬼，停止交易";
        NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
        content.setTitle(title)
                .setText(text);
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        request.setGroupValue("群组信息");
        request.setProgressBar(20, 100, true);
        request.setContent(notificationContent); // 设置通知的内容

        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException ex) {
//            HiLog.warn(LABEL, "publishNotification occur exception.");
        }
    }

    public static void cancelNotification() {
        try {
            NotificationHelper.cancelNotification(notificationId);
        } catch (RemoteException ex) {
//            HiLog.warn(LABEL, "cancelNotification occur exception.");
        }
    }

    //判断是否开启了通知权限
    public static boolean isAllowedNotifyNotice(){
        boolean allowedNotify = false;
        try {
            if (NotificationHelper.isAllowedNotify()) allowedNotify = true;
            else allowedNotify = false;
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return allowedNotify;
    }

  /*  //开启通知权限
    public static void setNotifyNotice(){
        try {
            boolean flag = NotificationHelper.isAllowedNotify();
            if(NotificationHelper.isAllowedNotify()){
                flag=true;
            }else {
                flag=false;
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    //关闭通知权限
    public static void setNotifyNoticeFalse(){
        try {
            boolean flag = NotificationHelper.isAllowedNotify();
            if(NotificationHelper.isAllowedNotify()){
                flag=false;
            }else {
                flag=true;
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }*/

}
